<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组排序-选择排序算法</title>
</head>

<body>
  <script>
    // 选择排序算法
    let arr = [4, 2, 5, 1]

    // //表示外层循环需要打印的次数
    for (let i = 0; i < arr.length; i++) {

      // 里层循环的每个元素都比较了5次
      for (let j = 0; j < arr.length - 1; j++) {
        // arr.length - 1=5
        if (arr[j] > arr[j + 1]) {
          let temp = arr[j]
          arr[j] = arr[j + 1]
          arr[j + 1] = temp
        }
      }
    }



    for (let i = 0; i < arr.length; i++) {

      // 里层循环的每个元素都比较了5次
      for (let j = 0; j < arr.length - 1 - i; j++) {
        // arr.length - 1=5
        if (arr[j] > arr[j + 1]) {
          let temp = arr[j]
          arr[j] = arr[j + 1]
          arr[j + 1] = temp
        }
      }
    }

  </script>
</body>

</html>